package common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import utils.GetFileMess;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import static io.restassured.RestAssured.given;
import static io.restassured.config.EncoderConfig.encoderConfig;

public class RunCaseJson {
    /*
     *post或get方式请求,返回响应报文（json格式）
     *@bodyString:json格式的请求报文体
     *@para:requestType post或get
     *@uri 接口地址
     */
    public static Response runCase(JSONObject json,String requestType,String uri){
        Response response = null;
        if(requestType.toLowerCase().equals("get"))
            response = given().log().all()
                    .contentType("application/json;charset=UTF-8")
                    .request()
                    .params(JSONObject.parseObject(json.toJSONString(), new TypeReference<Map<String, String>>(){}))  //把JSONObject转化成Map
                    .get(uri);
        else if(requestType.toLowerCase().equals("post"))
             response=given()
                     .contentType("application/x-www-form-urlencoded;charset=UTF-8")
                     .log()
                     .all()
                     .config(RestAssured.config().encoderConfig(encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
                     .params(json)
                     .when()
                     .post(uri);
        else  if (requestType.toLowerCase().equals("put"))
            response=given()
//                    .contentType("application/x-www-form-urlencoded")
                    .log()
                    .all()
                    .params(json)
                    .when()
                    .put(uri);
        else if (requestType.toLowerCase().equals("delete"))
        response=given()
//                    .contentType("application/x-www-form-urlencoded")
                .log()
                .all()
                .params(json)
                .when()
                .delete(uri);

            //打印格式化的参数
        //response.prettyPrint(); ////去掉部分日志 add by lrb 20181029


        return response;
    }

/*    public static void main(String[] args)  {
        try {
            String system = "env.properties";    //环境由filter配置
            RestAssured.baseURI = new GetFileMess().getValue("baseURI", system);
            RestAssured.basePath = new GetFileMess().getValue("basePath", system);
            RestAssured.port = Integer.parseInt(new GetFileMess().getValue("port", system));
            System.out.println(RestAssured.basePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject json= new JSONObject();
        json.put("userName", "112");
        json.put("passwd", "tP123456@");
        json.put("orgId", "75635");


        Response response=runCase(json,"post","api-auth/login");

        String jsonStr= response.asString();
        System.out.println(jsonStr);
        JSONObject obj = JSONObject.parseObject(jsonStr);
        JSONObject data= obj.getJSONObject("data");
        String accessToken=data.getString("accessToken");
        System.out.println(accessToken);

    }*/

}
